package com.hky.front_end_monitor.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hky.front_end_monitor.entity.SysErrorStack;
import com.hky.front_end_monitor.service.SysErrorStackService;
import com.hky.front_end_monitor.mapper.SysErrorStackMapper;
import com.hky.front_end_monitor.utils.NumberUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author hello
 * @description 针对表【sys_error_stack】的数据库操作Service实现
 * @createDate 2022-08-01 15:54:23
 */
@Service
public class SysErrorStackServiceImpl extends ServiceImpl<SysErrorStackMapper, SysErrorStack>
        implements SysErrorStackService {

    @Override
    public boolean storeErrorStack(String uuid, Map map) {
        ArrayList data =(ArrayList) ((LinkedHashMap) map.getOrDefault("stackTrace", map)).get("frames");
        if (data == null) return false;
        SysErrorStack sysErrorStack = new SysErrorStack();
        sysErrorStack.setErrVitalId(uuid);
        ArrayList<SysErrorStack> result= new ArrayList<>();
        data.forEach((_el) -> {
            LinkedHashMap<?,?> el = (LinkedHashMap<?, ?>) _el;
            sysErrorStack.setErrVitalId(uuid);
            sysErrorStack.setErrFile((String) el.get("filename"));
            sysErrorStack.setErrFunc((String) el.get("functionName"));
            sysErrorStack.setErrLine(NumberUtils.toInteger(el.get("lineno")));
            sysErrorStack.setErrCol(NumberUtils.toInteger(el.get("colno")));
            result.add(sysErrorStack);
        });
        return this.saveBatch(result);
    }
}




